﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;

namespace MII_FIF.Layouts.MII_FIF.Master.Router
{
    public partial class ViewRouter : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                btnAdd.Click += btnAdd_Click;
                btnDelete.Click += btnDelete_Click;
                GridRouter.RowCommand += GridRouter_RowCommand;
                GridRouter.PageIndexChanging += GridRouter_PageIndexChanging;
                //((Label)Header.FindControl("lblHeader")).Text = "View Router";
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void GridRouter_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridRouter.DataSource = ViewState["DTRouter"];
            GridRouter.PageIndex = e.NewPageIndex;
            GridRouter.DataBind();
        }

        void GridRouter_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Router/AddRouter.aspx?ID=" + e.CommandArgument));
            }
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool delete = false;
            DataTable DTRouter = (DataTable)ViewState["DTRouter"];
            InventoryManagement.Provider IM = new InventoryManagement.Provider();
            int stat = 0;
            foreach (GridViewRow row in GridRouter.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chk");
                if (chk != null)
                    if (chk.Checked)
                    {
                        delete = true;
                        break;
                    }
            }

            if (delete)
            {
                for (int i = GridRouter.Rows.Count - 1; i > -1; i--)
                {
                    CheckBox chk = (CheckBox)GridRouter.Rows[i].FindControl("chk");
                    if (chk != null)
                        if (chk.Checked)
                        {
                            DTRouter.Rows.RemoveAt(i);
                            IM.ID = Guid.Parse(GridRouter.Rows[i].Cells[2].Text.ToString());
                            IM.Title = GridRouter.Rows[i].Cells[1].Text.ToString();
                            IM.IsDeleted = true;
                            stat = IM.Update();
                            if (stat < 0)
                            {
                                lblError.Text += "Delete Failed"; break;
                            }
                        }
                }
                GridRouter.DataSource = DTRouter;
                GridRouter.DataBind();
                ViewState["DTProvider"] = DTRouter;
            }
            else
            {
                lblError.Text += "Please check at least 1 item to delete <br />";
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Router/AddRouter.aspx"));
        }

        void load()
        {
            InventoryManagement.Router IM = new InventoryManagement.Router();
            IM.IsDeleted = false;
            ViewState["DTRouter"] = IM.SelectByParam();
            GridRouter.DataSource = ViewState["DTRouter"];
            
            GridRouter.DataBind();
        }
    }
}
